/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package healthcare.coursework.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.*;

/**
 *
 * @author Administrator
 */
@Entity
@Table(name = "USERS")
@NamedQueries({
    @NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u")})
public class Users implements Serializable {

    private static final long serialVersionUID = 1L;
    private String username;
    private String password;
    private String fullName;
    private Date dateOfBirth;
    private String gender;
    private String phone;
    private String address;
    private String email;
    private Short experience;
    private String image;
    private String typeOfLaboratory;
    private boolean enabled;
    private List<Authorities> authorities;
    private Specialization specialization;
    private Outlet outlet;
    private List<Diagnoses> diagnoses;
    private List<TestSamples> testSamples;

    public Users() {
    }

    public Users(String username) {
        this.username = username;
    }

    public Users(String username, Date dateOfBirth, String gender, String phone, String address, boolean enabled) {
        this.username = username;
        this.dateOfBirth = dateOfBirth;
        this.gender = gender;
        this.phone = phone;
        this.address = address;
        this.enabled = enabled;
    }

    @Id
    @Column(name = "USERNAME", nullable = false, length = 20)
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Column(name = "PASSWORD", length = 30)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column(name = "FULL_NAME", length = 400)
    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    @Basic(optional = false)
    @Column(name = "DATE_OF_BIRTH", nullable = false)
    @Temporal(value = TemporalType.TIMESTAMP)
    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    @Basic(optional = false)
    @Column(name = "GENDER", nullable = false, length = 50)
    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Basic(optional = false)
    @Column(name = "PHONE", nullable = false, length = 30)
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Basic(optional = false)
    @Column(name = "ADDRESS", nullable = false, length = 500)
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Column(name = "EMAIL", length = 50)
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column(name = "EXPERIENCE")
    public Short getExperience() {
        return experience;
    }

    public void setExperience(Short experience) {
        this.experience = experience;
    }

    @Column(name = "IMAGE", length = 80)
    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    @Column(name = "TYPE_OF_LABORATORY", length = 80)
    public String getTypeOfLaboratory() {
        return typeOfLaboratory;
    }

    public void setTypeOfLaboratory(String typeOfLaboratory) {
        this.typeOfLaboratory = typeOfLaboratory;
    }

    @Basic(optional = false)
    @Column(name = "ENABLED", nullable = false)
    public boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user", fetch = FetchType.EAGER)
    public List<Authorities> getAuthorities() {
        return authorities;
    }

    public void setAuthorities(List<Authorities> authorities) {
        this.authorities = authorities;
    }

    @JoinColumn(name = "SPECIFICATION_ID", referencedColumnName = "SPECIALIZATION_ID")
    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
    public Specialization getSpecialization() {
        return specialization;
    }

    public void setSpecialization(Specialization specialization) {
        this.specialization = specialization;
    }

    @JoinColumn(name = "OUTLET_ID", referencedColumnName = "OUTLET_ID")
    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
    public Outlet getOutlet() {
        return outlet;
    }

    public void setOutlet(Outlet outlet) {
        this.outlet = outlet;
    }

    @OneToMany(mappedBy = "user", fetch = FetchType.EAGER)
    public List<Diagnoses> getDiagnoses() {
        return diagnoses;
    }

    public void setDiagnoses(List<Diagnoses> diagnosesList) {
        this.diagnoses = diagnosesList;
    }

    @OneToMany(mappedBy = "user", fetch = FetchType.EAGER)
    public List<TestSamples> getTestSamples() {
        return testSamples;
    }

    public void setTestSamples(List<TestSamples> testSamples) {
        this.testSamples = testSamples;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (username != null ? username.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Users)) {
            return false;
        }
        Users other = (Users) object;
        if ((this.username == null && other.username != null) || (this.username != null && !this.username.equals(other.username))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "healthcare.coursework.model.Users[username=" + username + "]";
    }

    @Transient
    public String getListAuthorities() {
        if(!getAuthorities().isEmpty())
        return getAuthorities().get(0).getAuthoritiesPK().getAuthority();
        else return "Not found";
    }

    public void addAuthority(Authorities a) {
        if (getAuthorities() != null) {
            getAuthorities().add(a);
        } else {
            List<Authorities> list=new ArrayList<Authorities>();
            list.add(a);
            setAuthorities(authorities);
        }
         a.setUser(this);
    }
}
